package com.gk.panda.apis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.panda.apis.entity.eventtodo.dto.HandelEventDto;
import com.gk.panda.apis.mapper.EventResourcesMapper;
import com.gk.panda.apis.service.EventResourcesService;
import com.gk.panda.pojo.EventResources;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: EventResourcesServiceImpl
 * @Description: 待办事件资源
 * @author: Sir.yang
 * @date: 2022/11/3 14:57
 * @version: 1.0
 **/
@Service
@Slf4j
public class EventResourcesServiceImpl extends ServiceImpl<EventResourcesMapper, EventResources> implements EventResourcesService {

    /**
     * @description: 根据事件ID获取事件资源信息
     * @param: []
     * @return: java.util.List<com.gk.panda.pojo.EventResources>
     * @date: 2022/11/3 17:25
     * @version: 1.0
     *
     * @param eventId*/
    @Override
    public List<EventResources> getEventResourcesListByEventId(Integer eventId) {
        QueryWrapper<EventResources> wrapper = new QueryWrapper<>();
        wrapper.eq(null != eventId,"event_id", eventId);
        return baseMapper.selectList(wrapper);
    }


    /**
     * @description: 保存事件资源
     * @param: [HandelEventDto]
     * @return: int
     * @date: 2022/11/3 23:39
     * @version: 1.0
     **/
    @Override
    public int saveResourceByHandelEvent(HandelEventDto dto) {
        List<EventResources> list = new ArrayList<>();
        if(!CollectionUtils.isEmpty(dto.getResourceAddress())){
            dto.getResourceAddress().forEach(eventDto ->{
                if(null != eventDto.getFileType() && StringUtils.isNotEmpty(eventDto.getFileUrl())){
                    EventResources resources = new EventResources();
                    resources.setEventId(dto.getEventId());
                    resources.setFileType(eventDto.getFileType());
                    resources.setFileUrl(eventDto.getFileUrl());
                    resources.setCreateTime(new Date());
                    resources.setCreateName(dto.getRecipientName());
                    resources.setFkCreate(dto.getFkRecipient());
                    list.add(resources);
                }
            });
            if(!CollectionUtils.isEmpty(list)){
                return baseMapper.insertList(list);
            }
        }
        return list.size();
    }

    /**
     * 保存事件资源
     * @param eventResources
     * @return
     */
    @Override
    public int saveResourceList(List<EventResources> eventResources) {
        return baseMapper.insertList(eventResources);
    }
}
